package core.subcontrollers;

import java.awt.Color;

import misc.MArrayList;
import entity.fields.properties.Street;

public class StreetGroup {
	MArrayList<Street> streets = new MArrayList<Street>();

	private Color color;

	public StreetGroup(Street street) {
		color = street.getColor();
		add(street);
	}

	public Color getColor() {
		return color;
	}

	public void add(Street street) {
		streets.add(street);
	}

	public boolean isFull(Street[] allStreets) {
		boolean isFull = false;
		int ownedNumber = SubController.getNumberOfStreetsWithSameColor(streets.getFirst(), streets.getArray());
		int totalNumber = SubController.getNumberOfStreetsWithSameColor(streets.getFirst(), allStreets);
		if (ownedNumber == totalNumber || hasAllBuildings())
			isFull = true;
		return isFull;
	}

	private boolean hasAllBuildings() {
		Street[] streetsArray = streets.getArray();
		for (int i = 0; i < streetsArray.length; i++) {
			if (!streetsArray[i].hasAllBuildings())
				return false;
		}
		return true;
	}

	public Street[] getStreets() {
		return streets.getArray();
	}

	public boolean streetCanBuild(Street street) {
	//	System.out.println("HAS ALL BUILDINGS " + street.hasAllBuildings());
		if (street.hasAllBuildings())
			return false;
		int top = 0;
		boolean sameLevel = true;
		Street[] streetsArray = streets.getArray();
		// top = the amount of buildings the streets with the highest amount of buildings got
		for (int i = 0; i < streetsArray.length; i++) {
			if (top < streetsArray[i].getBuildings())
				top = streetsArray[i].getBuildings();
		}
		// sameLevel = remains true if all streets contains the same amount of buildings, else false
		for (int i = 0; i < streetsArray.length; i++) {
			if (top != streetsArray[i].getBuildings()) {
				sameLevel = false;
				break;
			}
		}
	//	System.out.println("TOP and BUILDINGS " + top + " " + street.getBuildings());
	//	System.out.println("< TOP OR == LEVEL " + (street.getBuildings() < top) + " " + sameLevel);
		return (street.getBuildings() < top || sameLevel);
	}

}
